# 整体的菜单
import os.path
import matplotlib.pyplot as plt


lists = []


# 定义总类
class ZHICHU_hzt:

    def __init__(self, leibie, yuefen, jine):
        # 定义 类别 月份 金额
        self.leibie = leibie
        self.yuefen = yuefen
        self.jine = jine

    def menu_hzt(self):
        while 1:
            print("1-新增记录-（输入数字1）")
            print("2-查询支出-（输入数字2）")
            print("3-修改记录-（输入数字3）")
            print("4-统计支出-（输入数字4）")
            print("5-总览画图-（输入数字5）")
            print("0-退出系统-（输入数字0）")

            meenu_number = int(input("请输入数字选择功能："))

            if meenu_number == 1:
                print("欢迎使用新增记录功能")
                self.do_one_hzt()  # 新增记录功能
            elif meenu_number == 2:
                print("欢迎使用查询支出功能")
                self.do_tow_hzt()  # 查询支出功能
            elif meenu_number == 3:
                print("欢迎使用修改记录功能")
                self.do_three_hzt()  # 修改记录功能
            elif meenu_number == 4:
                print("欢迎使用统计支出功能")
                self.do_four_hzt()  # 统计支出功能
            elif meenu_number == 5:
                print("欢迎使用总览画图功能")
                self.do_five_hzt()  # 总览画图功能
            else:
                print("退出系统")
                return 0

    # 新增记录功能
    def do_one_hzt(self,):
        # 进行读取文件的操作
        # path 是文件的位置
        path = "/各式系统/考试题目/rec.txt"

        if not os.path.exists(path):
            print("文件不存在！")
        else:
            with open(path, "r") as f:
                index = 0
                heared = []
                for line in f:
                    print(line)
                    list = line.split(",")
                    list[-1] = list[-1].split("\n")[0]
                    print(list)
                    if index == 0:
                        heared = list
                        index += 1
                    else:
                        instance = lists(list[0], list[1], list[2])
                        self.lists.append(instance)
                        index += 1

        # 查找文件中与输入的是否重合
        list_leibie = input("请输入支出类别")
        list_yuefen = input("请输入支出月份")
        list_jine = input("请输入支出金额")

        # 缓存列表
        list1 =[]
        list1_1 = []

        list1 = [list_leibie, list_yuefen, list_jine]
        list1_1.append(list1)  # 存入列表
        for i in list1_1:
            if i[0] == list_leibie and i[1] == list_yuefen:
                print("数据已存在")
            else:
                list = [list_leibie, list_yuefen, list_jine]
                self.lists.append(list)

                # 进行文件的写入
                if not os.path.exists(path):
                    print("not in there")
                else:
                    with open(path,"a") as f:
                        line_1 = list_leibie + "," + list_jine + "," + list_jine
                        f.write(line_1)

                        # 进行排序
                        list_2 = []  # 缓存列表——2
                        for i in lists:
                            list_2.append(i[0])
                            list_2.append(i[1])
                            list_2.append(i[2])
                        # 缓存列表——3
                        list_3 = sorted(list_2)

    # 查询支出功能
    def do_tow_hzt(self):
        leibie_1 = input("请输入需要查找的类别")
        yuefen_1 = input("请输入需要查找的月份")

        path = "/各式系统/考试题目/rec.txt"

        if not os.path.exists(path):
            print("文件不存在！")
        else:
            with open(path, "r") as f:
                for i in lists:
                    if i[0] == leibie_1 and i[1] == yuefen_1:
                        return lists[2]


    # 修改记录功能
    def do_three_hzt(self):
        listTY = self.do_tow_hzt()
        if isinstance(listTY, list):
            list_leibie_1 = input("请输入要修改的类别")
            list_yuefen_1 = input("请输入要修改的月份")
            list_jine_1 = input("请输入要修改的金额")

            lists[0] = list_leibie_1
            lists[1] = list_yuefen_1
            lists[2] = list_jine_1

            path = "/各式系统/考试题目/rec.txt"
            if not os.path.exists(path):
                print("not in there")
            else:
                with open(path, "a") as f:
                    line_4 = list_leibie_1 + "," + list_jine_1 + "," + list_jine_1
                    f.write(line_4)
        else:
            print("所需要修改的数值，不存在")


    # 统计支出功能
    def do_four_hzt(self):
        sum = 0
        higth = 0
        for i in lists:
            sum += int(i[2])
            if int(i[2]) > higth:
                higth = int(i[2])
            else:
                continue
        # 这里求出最高支出
        for i in lists:
            if i[2] == higth:
                print("最高支出月份为：" + i[1] + "最高支出类别为：" + i[0])

    # 总览画图功能
    def do_five_hzt(self):
        # 中文
        plt.rcParams['font.sans-serif'] = ['SimHei']

        # 每个月的总支出的柱状图
        plt.subplots(231)
        plt.xlabel("月份")
        plt.ylabel("支出金额")
        plt.title("2021900427-黄梓庭-每个月的总支出的柱状图")
        x_tu1 = [1,2,3,4,5,6,7,8,9,10,11,]
        y_tu1 = [540,90,770,820,580,860,340,0,670,700,640]
        plt.bar(x=x_tu1, height=y_tu1)

        #全年电费、水果、超市、衣服总支出柱状图
        plt.subplots(232)
        plt.xlabel("类别名称")
        plt.ylabel("支出金额")
        plt.title("2021900427-黄梓庭-全年电费、水果、超市、衣服总支出柱状图")
        x_tu2 = ["电费","水果","超市","衣服"]
        y_tu2 = [1450,1700,1310,1390]
        plt.bar(x_tu2,height=y_tu2)





if __name__ == '__main__':
    tt = ZHICHU_hzt
    tt.menu_hzt(self=lists)